package com.xianggu.designMode.decorator.demo03;

import java.util.*;

/**
 * @author chengjy
 * @date 2025/3/19
 */
public class HistoryList<E> implements Set<E> {

    private List<E> list = new ArrayList<>();
    private  Set<E> subSet;

    public HistoryList(Set<E> subSet) {
        this.subSet = subSet;
    }

    @Override
    public int size() {
        return subSet.size();
    }

    @Override
    public boolean isEmpty() {
        return subSet.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return subSet.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        return subSet.add(e);
    }


    @Override
    public boolean remove(Object o) {
        boolean remove = subSet.remove(o);
        if (remove) {
            list.add((E) o);
        }
        return remove;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return subSet.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return subSet.addAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return subSet.retainAll(c);
    }
    @Override
    public boolean removeAll(Collection<?> c) {
        if (subSet.removeAll(c)) {
            list.addAll((Collection<? extends E>) c);
            return true;
        }
        return false;
    }
    @Override
    public void clear() {
        subSet.clear();
    }

    @Override
    public String toString() {
        return "HistoryList{\r\n" +
                "当前集合=" + subSet +
                "\r\n删除历史集合=" + list +
                '}';
    }
}
